package com.itheima.book.manager.dao;

import com.itheima.book.manager.domain.Book;

public class BookDao implements BaseBookDao {
//    Book[] books = new Book[5];
    private  static Book[] books = new Book[5];
    public boolean addBook(Book book){
//        Book[] books = new Book[5];
        int index = -1;
        for (int i = 0; i < books.length; i++) {
            if (books[i] == null){
                index = i;
                break;
            }
        }
        if (index != -1){
            books[index] = book;
            return true;
        }
        return false;
    }

    public Book[] getAllBooks(){
        return books;
    }

    public boolean deleteBookByBookId(String bookId){
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null){
                if (books[i].getBookId().equals(bookId)){
                    books[i] = null;
                    return true;
                }
            }
        }
        return false;
    }

    public void updateBookByBookId(Book book){
        for (int i = 0; i < books.length; i++) {
            if (books[i] != null) {
                if (books[i].getBookId().equals(book.getBookId())) {
                    books[i] = book;
                }
            }
        }
    }
}
